package gui.user.model;

import dao.ProductDao;
import dao.SaleDetailsDao;
import entity.Product;
import entity.SaleDetails;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.List;

public class SaleDetailsTableModel extends DefaultTableModel {
    SaleDetailsDao saleDetailsDao = new SaleDetailsDao();
    String[] tableHeaders = new String[]{"商品名称","商品单价","购买数量","价格"};//订单信息表头
    List saleDetailsList = null;
    String[][] saleDetailsData = null;
    public SaleDetailsTableModel(String saleId) throws Exception {
        saleDetailsList = saleDetailsDao.getAppointSaleDeatils(saleId);
        ProductDao productDao = new ProductDao();
        saleDetailsData = new String[saleDetailsList.size()][tableHeaders.length];
        for (int i = 0; i < saleDetailsList.size(); i++) {
            SaleDetails saleDetails = (SaleDetails)saleDetailsList.get(i);
            String productId = saleDetails.getProductId();
            Product product = productDao.getProductName(productId);
            saleDetailsData[i][0] = product.getProductName();
            saleDetailsData[i][1] = String.valueOf(product.getSalePrice());
            saleDetailsData[i][2] = String.valueOf(saleDetails.getSaleNumber());
            saleDetailsData[i][3] = String.valueOf((product.getSalePrice())*(saleDetails.getSaleNumber()));
        }
        this.setDataVector(saleDetailsData,tableHeaders);
    }
}
